package com.action.app_matter;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.enetiy.Balance;
import com.enetiy.Ticketuser;
import com.enetiy.Users;
import com.operation.TicketOper;
import com.service.DealService;
import com.service.LoginService;
import com.service.TicketService;
import com.util.DataUtil;
import com.util.Log4gUtil;
import com.util.PagingUilt;

public class TicketAction {
	private TicketService ticketService;
	private DealService dealService;
	private LoginService loginService;
	
	private String NF_callid;
	private String  token;
	
	private int pageSize;
	private int pageNum;
	private String params; 
	private int userid;
	private int ticketid;
	private PagingUilt<Ticketuser> pu  ;
	
	private void verify(){
		System.out.println(params);
		if(params!=null&&!params.equals("")){
			JSONObject dataParser = DataUtil.dataParser(params);
			if(dataParser.has("userid"))
				userid= dataParser.getInt("userid");
			if(dataParser.has("ticketid"))
				ticketid= dataParser.getInt("ticketid");
			
			if(dataParser.has("pageSize"))
				pageSize= dataParser.getInt("pageSize");
			if(dataParser.has("pageNum"))
				pageNum= dataParser.getInt("pageNum");
		}
	}
	
	/**
	 * 查询可领取券
	 */
		public void userTicket(){
			this.verify();
			
			Log4gUtil.Info("userTicket-进入查user券", userid+"", "params", params, NF_callid, token);
			
			TicketOper ticketOper = new TicketOper();
			HttpServletResponse response = ServletActionContext.getResponse();
			net.sf.json.JSONObject send =DataUtil.sendJson();
			PrintWriter out = null;
			try {
				out = response.getWriter();
				Users user = loginService.selectUserId(userid);
				JSONArray array = new JSONArray();
				if(user!=null){
					List<Ticketuser> st = ticketOper.seekTicket(ticketService, dealService, user);
					for (int i = 0; i < st.size(); i++) {
						 net.sf.json.JSONObject json = new net.sf.json.JSONObject();
						 json.put("ticketid", st.get(i).getId());
						 //赠送时长
						 int giveTime = st.get(i).getGiveTime()/60;
						 json.put("ticketName", st.get(i).getTicket().getTicketName());
						 json.put("giveTime",giveTime );
						 json.put("explain", st.get(i).getTicket().getExplain());
						 json.put("pic", st.get(i).getTicket().getPopupPic());
						 array.put(json);
					}
					send.put("code", 1);
					send.put("ticket", array.toString());
				}else{
					send.put("code", 0);
					send.put("ticket", array.toString());
					send.put("msg", "用户不存在");
				}
			} catch (Exception e) {
				send.put("code", -1);
				send.put("msg", "服务器异常");
				e.printStackTrace();
			}
			Log4gUtil.Info("userTicket-输出", userid+"", "return", send.toString(), NF_callid, token);
			out.print(send);
		}
		
	/**
	 * 
	 *领取券 	
	 */
		public void getTicket(){
			this.verify();
			Log4gUtil.Info("getTicket-进入领取券", userid+"", "params", params, NF_callid, token);
			HttpServletResponse response = ServletActionContext.getResponse();
			net.sf.json.JSONObject send =DataUtil.sendJson();
			PrintWriter out = null;
			try {
				out = response.getWriter();
				Ticketuser ticketuser = ticketService.selectTicketuser(ticketid);
				Users user = loginService.selectUserId(userid);
				if( ticketuser != null && user!=null ){
					Balance balance = dealService.selectBalance(userid);
					if(balance != null){
						int longtime = 	balance.getTicket04()+ticketuser.getGiveTime();
						balance.setTicket04(longtime);
						balance.setUpdateTime(new Date());
						dealService.updateBalance(balance);
					}else{
						balance = new Balance(user, "0", new Date(), 0,ticketuser.getGiveTime());
						dealService.insertBalance(balance);
					}
					ticketuser.setIsGet(1);
					ticketuser.setGetTime(new Date());
					ticketService.updateTickeUser(ticketuser);
					
					send.put("code", 1);
					send.put("msg", "领取成功");
				}else{
					send.put("code",0);
					send.put("msg", "参数有误");
				}
			} catch (Exception e) {
				send.put("code",-1);
				send.put("msg", "服务器异常");
				e.printStackTrace();
			}
			out.print(send);
			Log4gUtil.Info("getTicket-输出", userid+"", "return", send.toString(), NF_callid, token);
		}
		
		
		/**
		 * 券包列表
		 */
		public void ticketList(){
			this.verify();
			pu  = new PagingUilt<Ticketuser>();
			Log4gUtil.Info("ticketList-券包列表", userid+"", "params", params, NF_callid, token);
			
			if(pageNum!=0&&pageSize!=0){
				pu.setPageNum(pageNum);
				pu.setPageSize(pageSize);
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			HttpServletResponse response = ServletActionContext.getResponse();
			net.sf.json.JSONObject send =DataUtil.sendJson();
			PrintWriter out = null;
			try {
				out = response.getWriter();
				Users user = loginService.selectUserId(userid);
				JSONArray canGet = new JSONArray();//可领取
				JSONArray cannotGet = new JSONArray();//已领取
				if(user!=null){
					List<Ticketuser> st = ticketService.selectTicketuserList(userid, 0);//可领取
					if(!st.isEmpty()){
						for (int i = 0; i < st.size(); i++) {
							 net.sf.json.JSONObject json = new net.sf.json.JSONObject();
							 json.put("ticketid", st.get(i).getId());
							 //赠送时长
							 int giveTime = st.get(i).getGiveTime()/60;
							 json.put("giveTime",giveTime );
							 json.put("explain", st.get(i).getTicket().getExplain());
							 json.put("allotTime", sdf.format(st.get(i).getAllotTime()));
							 json.put("ticketName", st.get(i).getTicket().getTicketName());
							 canGet.put(json);
						}
					}
					ticketService.selectTicketuserPU(pu, userid);
					if(!pu.getList().isEmpty()){
						for (int i = 0; i < pu.getList().size(); i++) {
							Ticketuser t = pu.getList().get(i);
							net.sf.json.JSONObject json = new net.sf.json.JSONObject();
							 json.put("ticketid", t.getId());
							 //赠送时长
							 int giveTime = t.getGiveTime()/60;
							 json.put("giveTime",giveTime );
							 json.put("explain", t.getTicket().getExplain());
							 json.put("allotTime", sdf.format(t.getAllotTime()));
							 json.put("getTime", sdf.format(t.getGetTime()));
							 json.put("ticketName",t.getTicket().getTicketName());
							 cannotGet.put(json);
						}
					}
					send.put("code", 1);
					send.put("canGet", canGet.toString());
					send.put("cannotGet",cannotGet.toString());
					send.put("pageCount", pu.getPageCount());
				}else{
					send.put("code", 0);
					send.put("msg", "参数有误");
					send.put("canGet", canGet.toString());
					send.put("cannotGet",cannotGet.toString());
				}
			} catch (Exception e) {
				send.put("code", -1);
				send.put("msg", "服务器异常");
				e.printStackTrace();
			}
			Log4gUtil.Info("ticketList-券包列表", userid+"", "输出", send.toString(), NF_callid, token);
			out.print(send);
		}
		
		
		public void setTicketService(TicketService ticketService) {
			this.ticketService = ticketService;
		}

		public void setDealService(DealService dealService) {
			this.dealService = dealService;
		}

		public void setLoginService(LoginService loginService) {
			this.loginService = loginService;
		}

		public void setUserid(int userid) {
			this.userid = userid;
		}


		public void setNF_callid(String nF_callid) {
			NF_callid = nF_callid;
		}


		public void setToken(String token) {
			this.token = token;
		}


		public void setParams(String params) {
			this.params = params;
		}


		public void setTicketid(int ticketid) {
			this.ticketid = ticketid;
		}

		public void setPageSize(int pageSize) {
			this.pageSize = pageSize;
		}

		public void setPageNum(int pageNum) {
			this.pageNum = pageNum;
		}
		
}
